<?php

	//修改日期s************   需要修改日期的时候反注释这段代码
//	exit();
	if($_POST['data']!=NULL && $_POST['d']!=NULL){
		
		$new_data = json_decode($_POST['data'],true);
		
		$data = array_change($data,$new_data);
		
		file_put_contents($_POST['d'].'.json', json_encode($data));
		
		$result['data'] = $data;
		$result['message'] = "成功";
		$result['status'] = 200;
		echo json_encode($result);
		exit();
	}
	//修改日期e************
	
	
	
	//制作日期
	$y = '2020';
	if($_GET['y']!=NULL){
		$y = $_GET['y'];
	}
	$s = $y.'-01-01';
	
	global $weekarray;
	$weekarray = array("日","一","二","三","四","五","六");
	
	$time = strtotime($s);
	
	
	
	
	//数据来源 百度万年历
	$arr = array();
	for ($i=1; $i < 13; $i++) { 
		$str = $y.'年'.$i.'月';
		$r = file_get_contents('https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?tn=wisetpl&ie=utf8&oe=utf8&resource_id=39043&query='.$str);
		$r = json_decode($r,true);
		$data = array();
		$data = $r['data'][0]['almanac'];
		
		foreach ($data as $key => $value){
			if($value['month'] == $i){
				$key = $value['year'].'-'.sprintf("%02d",$value['month']).'-'.sprintf("%02d",$value['day']);
				$arr[$key] = $value;
			}
		}
	}
	
	
//	echo json_encode($arr);
//	exit();
	
	
	$data = array();
	while (date("Y",$time) == $y) {
		$ymd = date("Y-m-d",$time);
		$data[$ymd] = make($time);
		
		if($arr[$ymd]['value']!=null){
			$data[$ymd]['holiday'] = $arr[$ymd]['value'];
		}
		$data[$ymd] = array_change($data[$ymd],$arr[$ymd]);
		
		$time = $time + 86400;
	}
	
	$new_data = file_get_contents($y.'.json');
	$new_data = json_decode($new_data,true);
	
	
	$data = array_change($data,$new_data);
	
	//同步国家法定假日
	$work = file_get_contents($y.'work.json');
	$work = json_decode($work,true);
	$data = array_change($data,$work);
	
	
	file_put_contents($y.'.json', json_encode($data));
	echo json_encode($data);
	
	
	function make($time){
		$data['ymd'] = date("Ymd",$time);
		$data['Y'] = date("Y",$time);
		$data['M'] = date("M",$time);
		$data['m'] = date("m",$time);
		$data['D'] = date("D",$time);
		$data['d'] = date("d",$time);
		
		
		$w = date("w",$time);
		$data['weekday'] = '星期'.$GLOBALS['weekarray'][$w];
		$data['weekday_number'] = $w;
		
		//根据国家法定假日是否需要工作： 0 需要，1 不需要
		$data['is_working'] = 0;
		if($w==0 || $w==6){
			$data['is_working'] = 1;
		}
		
		//节日		
		$data['holiday'] = '';
		$data['desc'] = '';
		
		
		return $data;
	}
	
	
	function array_change($array, $change) {
		foreach($change as $key=>$v){
			if(is_array($change[$key])){
				$array[$key] = array_change($array[$key],$change[$key]);
			}else{
				$array[$key] = $v;
			}
		}
		return $array;
	}
	
	
	
?>